﻿using DBUtil;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL.Permission
{
    public class ModuleTypeDAL
    {
        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="order"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateOrder(int order, int id)
        {
            var sql = "UPDATE [Permission].[ModuleType] SET [Sort] = @M_Order WHERE [ModuleTypeID]=@M_Id";
            SqlParameter[] parameters = {
					new SqlParameter("@M_Order", order),
                    new SqlParameter("@M_Id", id)
			};
            int rows = DbHelperSQL.ExecuteSql(sql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public int GetCurrentOrder()
        {
            return DbHelperSQL.GetMaxID("Sort", "[Permission].[ModuleType]");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ModuleTypeID, string Name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[ModuleType]");
            strSql.Append(" where ModuleTypeID=@ModuleTypeID and [Name]=@Name");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleTypeID", ModuleTypeID),
                    new SqlParameter("@Name", Name)
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string Name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [Permission].[ModuleType]");
            strSql.Append(" where [Name]=@Name");
            SqlParameter[] parameters = {
                    new SqlParameter("@Name", Name)
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ModuleTypeEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Permission].[ModuleType](");
            strSql.Append("Name,Sort,Description)");
            strSql.Append(" values (");
            strSql.Append("@Name,@Sort,@Description)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,30),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Sort;
            parameters[2].Value = model.Description;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModuleTypeEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Permission].[ModuleType] set ");
            strSql.Append("Name=@Name,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("Description=@Description");
            strSql.Append(" where ModuleTypeID=@ModuleTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.NVarChar,30),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@Description", SqlDbType.NVarChar,50),
					new SqlParameter("@ModuleTypeID", SqlDbType.Int,4)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Sort;
            parameters[2].Value = model.Description;
            parameters[3].Value = model.ModuleTypeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ModuleTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[ModuleType] ");
            strSql.Append(" where ModuleTypeID=@ModuleTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleTypeID", SqlDbType.Int,4)
			};
            parameters[0].Value = ModuleTypeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ModuleTypeIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Permission].[ModuleType] ");
            strSql.Append(" where ModuleTypeID in (" + ModuleTypeIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModuleTypeEntity GetModel(int ModuleTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ModuleTypeID,Name,Sort,Description from [Permission].[ModuleType] ");
            strSql.Append(" where ModuleTypeID=@ModuleTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ModuleTypeID", SqlDbType.Int,4)
			};
            parameters[0].Value = ModuleTypeID;

            ModuleTypeEntity model = new ModuleTypeEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModuleTypeEntity DataRowToModel(DataRow row)
        {
            ModuleTypeEntity model = new ModuleTypeEntity();
            if (row != null)
            {
                if (row["ModuleTypeID"] != null && row["ModuleTypeID"].ToString() != "")
                {
                    model.ModuleTypeID = int.Parse(row["ModuleTypeID"].ToString());
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["Description"] != null)
                {
                    model.Description = row["Description"].ToString();
                }
            }
            return model;
        }
    }
}
